package com.my.ddd_sample_collections.association_object.south.adapter.repositories;

import com.my.ddd_sample_collections.association_object.domain.User;
import com.my.ddd_sample_collections.association_object.south.adapter.repositories.dao.UserDAO;
import com.my.ddd_sample_collections.association_object.south.port.repositories.UserRepository;

import java.util.List;
import java.util.stream.Collectors;

public class UserRepositoryDB implements UserRepository {
    private UserDAO userDao;

    public UserRepositoryDB(UserDAO userDao) {
        this.userDao = userDao;
    }
    @Override
    public User findById(long id) {
        User user = userDao.findById(id);
        return setMySubscription(user);
    }
    @Override
    public List<User> findBy(String sth) {
        List<User> users = null;
        return users.stream().map(user -> setMySubscription(user)).collect(Collectors.toList());
    }

    private User setMySubscription(User user) {
        user.setMySubscriptions(new MySubscriptionsDB(userDao, user));//通过DB获取数据
//        user.setMySubscriptions(new MySubscriptionsAPI());//另外一种实现，调用远程服务获取
        return user;
    }
}
